package org.androidmangher.controller;

import org.androidmangher.bean.User;
import org.androidmangher.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
//http://127.0.0.1:8081/hello
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/userList")
    public List<User> getUserList(){

        List<User> userList = userService.getUserList();
        return userList;
    }

    @GetMapping("/userListXML")
    public List<User> getUserListXML(){

        List<User> userList = userService.getUserList();
        return userList;
    }
    @PostMapping("/checkUser")
    public Map login(@RequestBody User user){

        System.out.println(user);

        User userinfo = userService.getOneUser(user);
        System.out.println(userinfo);
        HashMap<String, Object> message = new HashMap<>();

        if (userinfo==null){
            message.put("code","00");
            message.put("message","erro");
            message.put("userinfo",null);

        }else {
            message.put("code","01");
            message.put("message","sucess");
            message.put("userinfo",userinfo);

        }

        return message;
    } @PostMapping("/register")
    public Map register(@RequestBody User user){


        User userinfo = userService.insertOneUser(user);

        HashMap<String, Object> message = new HashMap<>();

        if (user==null){
            message.put("code","00");
            message.put("message","erro");
            message.put("data",null);

        }else {
            message.put("code","01");
            message.put("message","sucess");
            message.put("data",user);

        }

        return message;
    }

}
